import CustomTable from '@/components/CustomTable/index'
import CustomUpload from '@/components/CustomUpload/index'

import {
  SaveGroundBattleData,
  GetGroundBattleDataList,
  DeleteGroundBattleData,
  UploadGroundBattleData,
  UpdateGroundBattleData
} from '@/api/resourceManage/groundStation/index.js'

export default {
  components: {
    CustomTable,
    CustomUpload
  },
  data() {
    return {
      formInline: {
        pageCurrent: 1,
        pageSize: 10
      },
      total: 0,
      options: [
        {
          value: '值1',
          label: '选项1'
        },
        {
          value: '值2',
          label: '选项2'
        }
      ],
      tableData: [],
      currentPage3: 1,
      tableLoading: false,
      dialogVisible: false,
      uploadDialog: false,
      labelPosition: 'right',
      formLabelAlign: {
        dmzbs: undefined,
        txbs: undefined,
        txmc: undefined,
        zxgzyj: undefined,
        zxjsyj: undefined,
        zxckyj: undefined,
        dljd: undefined,
        dlwd: undefined,
        hbgd: undefined
      },
      taskCaseBool: 0,
      columns: [
        { label: '地面战标识', prop: 'dmzbs' },
        { label: '天线标识', prop: 'txbs' },
        { label: '天线名称', prop: 'txmc' },
        { label: '接收仰角', prop: 'zxjsyj' },
        { label: '经度', prop: 'dljd' },
        { label: '纬度', prop: 'dlwd' },
        { label: '高度', prop: 'hbgd' },
        { label: '操作', prop: 'action' }
      ],
      rules: {
        dmzbs: [
          { required: true, message: '请输入地面战标识', trigger: 'blur' }
        ],
        txbs: [{ required: true, message: '请输入天线标识', trigger: 'blur' }],
        txmc: [{ required: true, message: '请输入天线名称', trigger: 'blur' }],
        zxjsyj: [
          { required: true, message: '请输入接收仰角', trigger: 'blur' }
        ],
        dljd: [
          { required: true, message: '请输入经度', trigger: 'blur' },
          {validator:this.jdCoolarInfo,message: '经度格式不正确',trigger: 'blur'}
        ],
        dlwd: [{ required: true, message: '请输入纬度', trigger: 'blur' },
          {validator:this.wdCoolarInfo,message: '纬度格式不正确',trigger: 'blur'}],
        hbgd: [{ required: true, message: '请输入高度', trigger: 'blur' }]
      },
      idsArr: [],
      excelFile: ''
    }
  },
  computed: {},
  created() {},
  mounted() {
    this.GetGroundBattleDataList()
  },
  methods: {
    // 效验经度坐标格式
    jdCoolarInfo(rule,value,callback){
      const coordinateRegex = /^[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/;
      if(coordinateRegex.test(value)){
        callback();
      }
      else{
        callback(new Error('经度格式不正确'))
      }
    },
    // 效验纬度坐标格式
    wdCoolarInfo(rule,value,callback){
      const coordinateRegex = /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)$/;
      if(coordinateRegex.test(value)){
        callback();
      }
      else{
        callback(new Error('经度格式不正确'))
      }
    },
    closeModal() {
      this.$refs.fileRef.clearFileList()
      this.uploadDialog = false
    },
    async uploadData() {
      if (!this.excelFile) {
        this.$message({
          type: 'error',
          message: '请选择上传文件'
        })
        return false
      }
      this.$message.warning("正在导入")
      try {
        const fileData = new FormData()
        fileData.append('file', this.excelFile.raw)

        UploadGroundBattleData(fileData).then(resposne => {
          if (resposne.data.code !== 200) {
            this.$message({
              type: 'success',
              message: '上传失败'
            })
          }
          this.uploadDialog = false
          this.GetGroundBattleDataList()

          this.$message({
            type: 'success',
            message: '上传成功'
          })
        })
      } catch (error) {
        this.$message.error(error)
      }
    },
    fileCallback(file) {
      this.excelFile = file
    },
    // 上传excel
    handleImportExcel() {
      this.uploadDialog = true
    },
    //查询
    onSubmit() {
      this.formInline.pageCurrent = 1
      this.GetGroundBattleDataList()
    },
    // 重置
    onReset() {
      this.formInline = {}
      this.formInline.pageCurrent = 1
      this.GetGroundBattleDataList()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.formInline.pageCurrent = val
      this.GetGroundBattleDataList()
    },
    /**
     * 默认为新建任务案例
     * @param val
     */
    addNewTaskCase(val) {
      this.dialogVisible = true
      this.taskCaseBool = val
    },
    /** 保存观测卫星提交 */
    saveTaskCase(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) return false
        this.dialogVisible = false
        this.taskCaseBool = 0
        if(!this.formLabelAlign.id || this.formLabelAlign.id==''){
          SaveGroundBattleData(this.formLabelAlign).then(resposne => {
            if (resposne.data.code == 200) {
              this.$message({
                type: 'success',
                message: '操作成功'
              })
              this.formInline.pageCurrent = 1
              this.GetGroundBattleDataList()
            } else {
              this.$message({
                type: 'error',
                message: '操作失败'
              })
            }
            this.formLabelAlign = {}
          })
        }
        else{
          this.UpdateDmzData()
        }

      })
    },
    // 更新地面站数据
    UpdateDmzData(){
      UpdateGroundBattleData(this.formLabelAlign).then(res=>{
        if (res.data.code == 200) {
          this.$message({
            type: 'success',
            message: '操作成功'
          })
          this.formInline.pageCurrent = 1
          this.GetGroundBattleDataList()
        } else {
          this.$message({
            type: 'error',
            message: '操作失败'
          })
        }
      })
    },
    /** 获取观测卫星列表 */
    GetGroundBattleDataList() {
      this.tableLoading = true
      GetGroundBattleDataList(this.formInline).then(resposne => {
        if (resposne.data.code == 200) {
          this.tableData = resposne.data.data.records
          this.total = resposne.data.data.total
        } else {
          this.$message({
            type: 'error',
            message: '操作失败'
          })
        }
        this.tableLoading = false
      })
    },
    /** 取消按钮 */
    cancel(ruleForm) {
      this.$refs[ruleForm].clearValidate()
      this.formLabelAlign = {}
      this.dialogVisible = false
    },
    /** 删除单个卫星数据 */
    DeleteGroundBattleData(row) {
      let param = {
        ids: []
      }
      if (row.id) {
        param.ids.push(row.id)
      } else {
        param.ids = this.idsArr.map(item => {
          return item.id
        })
      }
      if (param.ids.length == 0) {
        this.$message({
          type: 'warning',
          message: '请选择删除项'
        })
        return false
      }

      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        type: 'warning'
      }).then(() => {
        DeleteGroundBattleData(param).then(resposne => {
          if (resposne.data.code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.GetGroundBattleDataList()
          } else {
            this.$message({
              type: 'error',
              message: '删除失败'
            })
          }
        })
      })
    },
    EditObservationData(val){
      this.dialogVisible=true
      this.formLabelAlign={}
      this.taskCaseBool=0
      Object.keys(val).forEach(key=>{
        this.$set(this.formLabelAlign,key,val[key])
      })
    },
    /** 表格复选框 */
    handleSelectionChange(val) {
      this.idsArr = val
    },
    // 效验是否是数字
    handleInput(event, name) {
    }
  }
}
